package com.kirayous.handler;


import com.kirayous.common.annotation.PagePathPatternsAno;
import com.kirayous.common.utils.PagePathsUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @author KiraYous
 * @version V1.0
 * @Package com.kirayous.handler
 * @date 2021/10/20 5:03
 */
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {


    //前置处理器
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }


    //后置处理器,筛选由PagePathPatternsAno修饰的类，并获取到path
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        Class<?> targetCls = bean.getClass();
        Method [] methods=targetCls.getMethods();
        for (Method method : methods) {
            if(method.isAnnotationPresent(PagePathPatternsAno.class)){
                PagePathPatternsAno pathAno= method.getAnnotation(PagePathPatternsAno.class);
               String path=pathAno.path();
               PagePathsUtils.pathSet.add(path);
//                System.out.println("这里add一个path: "+path);
            }
        }
        return bean;
    }


}
